﻿Imports System.Net.Mail

Public Class PasswordRecovery
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub RecoverButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles recoverButton.Click

        Dim userID, email As String

        userID = userIdBox.Text
        email = userEmailBox.Text

        Dim valid As String ' = select password from employee where userID=userName and emailAddress=email


        'if the 'valid' is returned then it means that the userId adn email match and return a password for the user
        If (valid IsNot Nothing) Then

            Dim [to] As New MailAddress("cosc320twin@gmail.com")
            Dim [from] As New MailAddress("cosc320twin@gmail.com")
            Dim mail As New MailMessage([from], [to])
            mail.Subject = "Password Recovery"
            mail.Body = "This message is to reset your password. If you did not request this message, then please contact the system admin at once.\n\nUser: " & userID & " Password: " & valid
            mail.IsBodyHtml = True

            Dim smtp As SmtpClient = New SmtpClient()
            smtp.Host = "smtp.gmail.com"
            smtp.Port = 587
            smtp.Credentials = New System.Net.NetworkCredential("cosc320twin@gmail.com", "shumbashumba")
            smtp.EnableSsl = True
            smtp.Send(mail)

            MsgBox("Password retrieval sent to the email that you are registered with. Redirecting to Login.")
            Response.Redirect("Login.aspx")

        Else
            'error message
            MsgBox("Incorrect credentials. Please try again.")


        End If



    End Sub



    Protected Sub BackButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles backButton.Click
        Response.Redirect("Login.aspx")
    End Sub

    Protected Sub ClearButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles clearButton.Click

        userIdBox.Text = ""
        userEmailBox.Text = ""

    End Sub
End Class